package com.gwtjs.icustom.controller;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.gwtjs.icustom.entity.ResultWrapper;
import com.gwtjs.icustom.pojo.ServerHost;
import com.gwtjs.icustom.pojo.User;
import com.gwtjs.icustom.utils.ImportExcelUtil;

@RestController @RequestMapping("/upload")
public class UploadController {
	
	private final static Logger logger = LoggerFactory.getLogger(UploadController.class);
	
    @Resource
    private ImportExcelUtil excelUtils;
    
    @PostMapping("/host")
    public ResultWrapper upload(MultipartFile excelFile) throws Exception{
        Map<String,String> map = new HashMap<>();
        List<ServerHost> hostList = null;
        if (excelFile.isEmpty()){
            map.put("mag","文件夹为空，重新上传");
        }else {
            String fileName = excelFile.getOriginalFilename();
            InputStream is = excelFile.getInputStream();
            Workbook workbook = excelUtils.getWorkbook(fileName,is);
            hostList = excelUtils.excelToServerHostObject(workbook);
            logger.info("\n{}\n",hostList);

        }

        return ResultWrapper.constructSuccessResult(hostList);
    }

    @PostMapping("/user")
    public Map<String,String> uploadUser(MultipartFile excelFile) throws Exception{
        Map<String,String> map = new HashMap<>();
        if (excelFile.isEmpty()){
            map.put("mag","文件夹为空，重新上传");
        }else {
            String fileName = excelFile.getOriginalFilename();
            InputStream is = excelFile.getInputStream();
            Workbook workbook = excelUtils.getWorkbook(fileName,is);
            List<User> userList = excelUtils.excelToUserObject(workbook);
            System.out.println(userList);

        }

        return map;
    }
}

